package com.epichust.bootumpp.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.epichust.bootumpp.model.dao.Mbf5ProduceOpMresourceDao;
import com.epichust.bootumpp.model.dao.Mbf5ProduceOperationDao;
import com.epichust.bootumpp.model.entity.Mbf5ProduceOpEresource;
import com.epichust.bootumpp.model.entity.Mbf5ProduceOpMresource;
import com.epichust.bootumpp.model.entity.Mbf5ProduceOperation;
import com.epichust.bootumpp.service.IMbf5ProduceOpMresourceService;
import com.epichust.bootumpp.service.IMbf5ProduceOperationService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class Mbf5ProduceOpMresourceServiceImpl extends ServiceImpl<Mbf5ProduceOpMresourceDao, Mbf5ProduceOpMresource> implements IMbf5ProduceOpMresourceService {


    @Override
    public List<Mbf5ProduceOpMresource> getOpMresourceByParams(Map<String, String> map)
    {
        String mrlFlag = map.get("mrlFlag");
        String opGid = map.get("opGid");
        QueryWrapper<Mbf5ProduceOpMresource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("IS_DELETE", 0).eq("OP_GID",opGid).eq("MRL_FLAG", mrlFlag);
        List<Mbf5ProduceOpMresource> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public void saveOpMresource(Map<String, String> map) {
        Gson gson = new GsonBuilder().serializeNulls().create();
        String user = map.get("user");
        String opGid = map.get("opGid");
        String rowData = map.get("rowData");
        //删除
//        QueryWrapper<Mbf5ProduceOpMresource> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("OP_GID", opGid);
//        boolean f = this.remove(queryWrapper);
        //修改 isDelete=1
        UpdateWrapper<Mbf5ProduceOpMresource> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("OP_GID", opGid).set("IS_DELETE", 1);
        boolean f2 = this.update(updateWrapper);


        List<Map<String, Object>> mapList = gson.fromJson(rowData, List.class);
        List<Mbf5ProduceOpMresource> list = new ArrayList<Mbf5ProduceOpMresource>();
        for (int i=0; i<mapList.size(); i++)
        {
            Map<String, Object> data = mapList.get(i);
            Mbf5ProduceOpMresource mresource = new Mbf5ProduceOpMresource();

            String mrlGid = data.get("mrlGid").toString();
            String mrlCode = data.get("mrlCode").toString();
            String mrlName = data.get("mrlName").toString();
            Double mrlFlag = (Double) data.get("mrlFlag");
            String mrlClassName = data.get("mrlClassName").toString();
            Double masterFlag = (Double) data.get("masterFlag");
            Integer qty = null;
            if(data.get("id") == null){
                qty = Integer.valueOf(data.get("qty").toString());
            }else {
                qty = ((Double) data.get("qty")).intValue();
            }
            String ver = data.get("ver").toString();

            //mresource.setId(i+1000);
            mresource.setOpGid(opGid);
            mresource.setMrlGid(mrlGid);
            mresource.setMrlCode(mrlCode);
            mresource.setMrlName(mrlName);
            mresource.setMrlClassName(mrlClassName);
            mresource.setMrlFlag(mrlFlag.intValue());
            mresource.setMasterFlag(masterFlag.intValue());
            mresource.setQty(qty);
            mresource.setVer(ver);
            mresource.setCreateId(user);
            mresource.setCreateDate(new Date());
            mresource.setModifyId(user);
            mresource.setModifyDate(new Date());
            list.add(mresource);

        }
        this.saveBatch(list);
    }
}
